package ui;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;

/**
 * Created by IntelliJ IDEA.
 * User: sebastienpelurson
 * Date: 28/02/12
 * Time: 11:22
 * To change this template use File | Settings | File Templates.
 */
public class ImagePanel extends JPanel {
    private BufferedImage image;

    public ImagePanel(BufferedImage img, int width, int height)
    {
        this.image = img;
        this.setPreferredSize(new Dimension(width, height));
    }
    
    public void drawText(String s) {
        int w = image.getWidth();
        int h = image.getHeight();
        BufferedImage img = new BufferedImage(
            w, h, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = img.createGraphics();
        g2d.drawImage(image, 0, 0, null);
        g2d.setPaint(Color.black);
        g2d.setFont(new Font("Serif", Font.BOLD, 70));
        FontMetrics fm = g2d.getFontMetrics();
        int x = (500 - fm.stringWidth(s))/2;
        int y = 300;
        g2d.drawString(s, x, y);
        g2d.dispose();
        this.image = img;
    }

    public void paintComponent(Graphics g)
    {
        Graphics g2 = g.create();
        g2.drawImage(image, 0, 0, getWidth(), getHeight(), null);
        g2.dispose();
    }
}
